package com.apples.mylibrary;

import android.Manifest;
import android.app.Activity;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.File;

public class HexViewerActivity extends AppCompatActivity {

    private boolean ifs = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hex_viewer);
        requestPermissions();
        register();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (ifs) {
            ifs = false;
            String lastFilePath = getStringSP(this, "hexvr_last_file_path", null);
            if (TextUtils.isEmpty(lastFilePath) || !new File(lastFilePath).exists()) {
                activityResultLauncherOpen.launch(
                        Intent.createChooser(FileHelper.prepareForOpenFile(), "选择要打开的一个文件"));
            } else {

            }
        }
    }

    private ActivityResultLauncher<Intent> activityResultLauncherOpen;

    private void register() {
        activityResultLauncherOpen = registerForActivityResult(
                new ActivityResultContracts.StartActivityForResult(),
                result -> {
                    if (result.getResultCode() == Activity.RESULT_OK) {
                        Intent data = result.getData();
                        if (data != null) {
                            if (FileHelper.takeUriPermissions(this, data.getData(), false)) {
                                processFileOpen(new FileData(this, data.getData(), false));
                            }
                        }
                    }
                });
    }

    private void processFileOpen(final FileData fd) {
        if (fd != null && fd.getUri() != null && fd.getUri().getPath() != null) {

        }
    }


    public static void putBooleanSP(Context context, String k, boolean v) {
        if (context == null || TextUtils.isEmpty(k)) return;
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putBoolean(k, v);
        editor.apply();
    }

    public static boolean getBooleanSP(Context context, String k, boolean def) {
        if (context == null || TextUtils.isEmpty(k)) return def;
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPref.getBoolean(k, def);
    }

    public static void putStringSP(Context context, String k, String v) {
        if (context == null || TextUtils.isEmpty(k) || TextUtils.isEmpty(v)) return;
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPref.edit();
        editor.putString(k, v);
        editor.apply();
    }

    public static String getStringSP(Context context, String k, String def) {
        if (context == null || TextUtils.isEmpty(k)) return def;
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        return sharedPref.getString(k, def);
    }

    private void requestPermissions() {
        boolean requestPermissions = Build.VERSION.SDK_INT > Build.VERSION_CODES.Q
                && (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
                || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED);
        if (requestPermissions)
            ActivityCompat.requestPermissions(this, new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE
            }, 1);
    }

}